package com.onlinemusic.controller;

import com.onlinemusic.common.R;
import com.onlinemusic.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 支付宝沙箱支付接口
 */
@Controller
@RequestMapping("pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 通用支付
     *
     * @param songIds
     * @param userId
     * @param payType
     * @return
     * @throws Exception
     */
    @PostMapping("common/alipay")
    @ResponseBody
    public R commonAlipay(@RequestParam(value = "songIds") List<Integer> songIds,
                          @RequestParam("userId") Integer userId,
                          @RequestParam("payType") Integer payType) throws Exception {
        return payService.commonAlipay(songIds, userId, payType);
    }

    /**
     * 回调跳转页面
     *
     * @param response
     * @throws Exception
     */
    @GetMapping("common/return")
    public void commonAlipayReturn(@RequestParam("out_trade_no") String tradeNo,
                                   @RequestParam("sign") String sign,
                                   HttpServletResponse response) throws Exception {
        payService.commonPayReturn(tradeNo, sign, response);
    }

    /**
     * 回调核销订单子项
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("common/notify")
    @ResponseBody
    public R commonAlipayNotify(HttpServletRequest request) throws Exception {
        return payService.commonPayNotify(request);
    }

    /**
     * 返回所有已支付的歌曲清单
     *
     * @return
     */
    @GetMapping("allPayList")
    @ResponseBody
    public R getPayList(@RequestParam("userId") Integer userId) {
        return payService.allPayList(userId);
    }
}
